package com.gsoe.problem.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gsoe.problem.po.Exam;
import com.gsoe.problem.vo.NeedPeopleProblemVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 考试表 Mapper 接口
 * </p>
 *
 * @author xiaoma
 * @since 2024-10-27
 */
public interface ExamMapper extends BaseMapper<Exam> {
    @Insert("insert into exam(exam_name, begin_time, end_time, validity,score, class_belong, create_time) " +
            "select #{exam.examName},#{exam.beginTime},#{exam.endTime},#{exam.validity},#{exam.score},#{exam.classBelong},#{exam.createTime} from (select 1) as d " +
            "where NOT EXISTS(" +
            "select 1 from exam " +
            "where exam_name=#{exam.examName}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer insertWithLock(@Param("exam") Exam exam);


    @Select("""
            select
                p.problem_describe as problemDescribe,
                p.id AS id,
                ep.score as score,
                group_concat(pa.answer separator '\n') AS answer
            from
                exam_problem as ep
            inner join
                problem p ON ep.problem_id = p.id
            left join
                problem_answer AS pa ON p.id = pa.problem_id
            where
                ep.exam_id = #{examId} and ep.need_people=true
            group by
                p.problem_describe, p.id,ep.score
            order by
                p.id ;
            """)
    List<NeedPeopleProblemVO> getNeedProblemList(@Param("examId") Long examId);

    @Select("select  id, exam_name, begin_time, end_time, validity, class_belong, create_time, update_time, update_user_name, score, notes\n" +
            "from exam where id=#{examId}")
    Exam getExamInfo(@Param("examId") Long examId);
}
